package com.pl.lister;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.pl.entity.SyslogTb;
import com.pl.service.SyslogTbService;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @Author 小胤
 * @Date 2024/5/5 0:29
 * @注释
 */
public class SysLogReadLister extends AnalysisEventListener<SyslogTb> {
    private static final int BATCH_COUNT=5;
    private SyslogTbService syslogTbService;
    List<SyslogTb>list=new ArrayList<>();

    public SysLogReadLister() {
    }

    public SysLogReadLister(SyslogTbService syslogTbService) {
        this.syslogTbService = syslogTbService;
    }

    @Override
    public void invoke(SyslogTb syslogTb, AnalysisContext analysisContext) {
        list.add(syslogTb);
        if (list.size()>=BATCH_COUNT){
            saveData();
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        if (list.size()>0){
            saveData();
            list.clear();
        }
    }
    private void saveData() {
        System.out.println("开始批量导入");
        syslogTbService.inserts(list);
        System.out.println("结束批量导入");
    }
}
